package com.zhentao.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.aop.MyLog;
import com.zhentao.domain.SErrorBook;
import com.zhentao.domain.dto.ErrorBookDto;
import com.zhentao.result.Result;
import com.zhentao.service.SErrorBookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/errorBook")
public class ErrorBookController {
    @Resource
    private SErrorBookService errorBookService;

    /**
     * 查询错题本列表
     * @param dto
     * @return
     */
    @PostMapping("/getErrorBookList")
    @MyLog(title = "错题本",content = "查询错题本")
    public Result getErrorBookList(@RequestBody ErrorBookDto dto){
        Page<SErrorBook> page = errorBookService.getErrorBookList(dto);
        return Result.OK(page);
    }

    /**
     * 删除错题
     * @param qid
     * @param uid
     * @return
     */
    @GetMapping("/getErrorBookDel/{qid}/{uid}")
    @MyLog(title = "错题本",content = "删除错题")
    public Result getErrorBookDel(@PathVariable("qid")Integer[] qid,@PathVariable("uid")Integer uid){
        List<SErrorBook> bookList = new ArrayList<>();
        List<SErrorBook> list = errorBookService.list(Wrappers.<SErrorBook>lambdaQuery().eq(SErrorBook::getUserId, uid));
        for (Integer integer : qid) {
            for (SErrorBook book :list){
                if (Objects.equals(book.getQuestionId(), integer)){
                    bookList.add(book);
                }
            }
        }
        boolean b = errorBookService.removeBatchByIds(bookList);
        if (b){
            return Result.OK("删除成功");
        }else {
            return Result.ERROR("删除失败");
        }
    }
}